﻿/// <reference path="../jquery-1.4.1-vsdoc.js" />
$.fx.speeds._default = 400;
var order = { "straight": 0, "up": 1, "down": 3 };
var currentOrder = order.straight;

$(document).ready(function () {
    loadUI();
    $("input:submit", "#createGame").button();
    $("#refreshGameList").button();
    $("#createGameButton").button();
    $("#createGameButton").click(function () {
        $("#createGameDialog").dialog("open");
        return false;
    });
});

function loadUI() {
    $("#createGameDialog").dialog({
        autoOpen: false,
        draggable: false,
        resizable: false,
        show: 'slide',
        hide: 'clip'
    });
    
    $("#sortUp").button();
    $("#sortDown").button();

    $("#sortUp").click(function () {
        updateGamesList('/Lobby/GamesListUp');
        currentOrder = order.up;
        return false;
    });

    $("#sortDown").click(function () {
        updateGamesList('/Lobby/GamesListDown');
        currentOrder = order.down;
        return false;
    });

    $("input:submit").button();
    $("input:submit").css('font-size', '0.75em');
}

function updateGamesList(updateUrl) {
    $.ajax({
        url: updateUrl,
        success: function (data) {
            $("#gamesList").html(data);
            loadUI();
        }
    });
}

function updateGamesListByOrder() {
    switch (currentOrder) {
        case order.straight: updateGamesList('/Lobby/GamesList'); break;
        case order.up: updateGamesList('/Lobby/GamesListUp'); break;
        case order.down: updateGamesList('/Lobby/GamesListDown'); break;
    }
}

function updateUserGamesList() {
    $.ajax({
        url: '/Lobby/UserGamesList',
        success: function (data) {
            $("#userGamesList").html(data);
            $("input:submit").button();
            $("input:submit").css('font-size', '0.75em');
        }
    });
}

function joinGame(id) {
    $.ajax({
        type: "POST",
        url: '/Room/Join/'+id,
        success: function () {
            document.location = '/Room/Game/'+id;
        }
    });
}

function backToGame(id) {
    document.location = '/Room/Game/' + id;
}